<?
session_start();
//session_register('kart_array');
//$_SESSION['kart_array'] ;

$path = '../Mkart/';
include($path."common.php");
include($path."prod_fun.php");

$id_array = $_GET['pid'] ;
$num_array = $_GET['num'] ;
$count_array = $_GET['count'] ;

//修改數量
if(isset($id_array) and isset($count_array) and isset($num_array)){
$sql_count = "select * from product where id = '$id_array' order by id" ;
$result_count = mysql_query($sql_count) ;
$row_count = mysql_fetch_object($result_count) ;
$price_count = $row_count -> price_2 ;

$total_price = $price_count * $count_array ;
$_SESSION['kart_array'][$num_array]['price'] = $total_price ;
$_SESSION['kart_array'][$num_array]['count'] = $count_array ;
}

if($_SESSION['kart_array']){

$kart_list  .= "<form method=post id=frm_list name=frm_list action=\"\">" ;
$kart_list  .= "<table class=\"shopping_list\" width=100% border=0>" ;
$kart_list  .= "<tr class=shoppingcart_list_bg>" ;
$kart_list  .= "<td align=center class=list_name>"."商品名稱"."</td>" ;
$kart_list  .= "<td align=center class=list_amount>"."數量"."</td>" ;
$kart_list  .= "<td align=center class=list_unit_price>"."單價"."</td>" ;
$kart_list  .= "<td align=center class=list_sum>"."小計"."</td>" ;
$kart_list  .= "<td align=center class=list_cancel_order>"."取消訂購"."</td>" ;
$kart_list  .= "<td align=center>".""."</td>" ;
$kart_list  .= "</tr>" ;
$kart_list  .= "<tr class=list_padding><td colspan=4><td></tr>" ;

for($i=0;$i<count($_SESSION['kart_array']);$i++){

$prodid = $_SESSION['kart_array'][$i]['id'] ;
$prodname = $_SESSION['kart_array'][$i]["name"] ;
$prodcount = $_SESSION['kart_array'][$i]["count"] ;
$prodprice = $_SESSION['kart_array'][$i]['price'] ;

$sql = "select * from product where id = '$prodid' order by id" ;
$result = mysql_query($sql) ;
$row = mysql_fetch_object($result) ;
$qty = $row -> pd_count ;
if($qty <= 0){
$_SESSION['kart_array'] = del_array('id',$prodid,$_SESSION['kart_array']) ;
}elseif($qty > 100){
$qty = 100 ;
}


//取得DOCUMENT ROOT到專案"小烏龜"index.php的距離。
include('Class_Route.php');
$route_tortoisePrj = new route();
//變數名我隨便取，因為這個方法還不夠成熟，但已經足夠了。
$str_tortoisePrjDistance = $route_tortoisePrj->getTortoisePrjDistance();



$kart_list  .= "<tr class=list_value>" ;
$kart_list  .= "<td class=list_product_name>".$_SESSION['kart_array'][$i]["name"]."</td>" ;
$kart_list  .= "<td align=center><select name=count[$prodid] onchange=\"change_count('$prodid',$i,this.value)\">" ;
$kart_list  .= "<option value=\"$prodcount\">".$_SESSION['kart_array'][$i]['count']."</option>" ;
for($m=1 ; $m<=$qty ; $m++){
$kart_list  .= "<option value='$m'>".$m."</option>" ;
}
$kart_list  .= "</td>" ;

$kart_list  .= "<td  class=list_unit_price_value align=center >"."NT$".$_SESSION['kart_array'][$i]['price']/$_SESSION['kart_array'][$i]['count']."</td>" ;

$kart_list  .= "<td  class=list_price align=center>"."NT$".$_SESSION['kart_array'][$i]['price']."</td>" ;


$kart_list  .= "<td align=right>"."<input class=cancel  name=del_$i type=button onclick=\"cancel_prod($i)\" value=\"取消\">"."</td>" ;
$kart_list  .= "</tr>" ;
$kart_list  .= "<tr><td colspan=4><td></tr>" ;
}
$kart_list  .= "</table>" ;


$kart_list  .= "<table width=710px border=0>" ;
$kart_list  .= "<tr><td class=list_greenline ><td></tr>" ;
$kart_list  .= "</table>" ;



$price_sum  = count_array('price',$_SESSION['kart_array']) ;
$kart_list  .= "<table width=100% border=0>" ;
$kart_list  .= "<tr class=list_value>" ;
$kart_list  .= "<td class=list_total_price align=right width=150px >總價<td>" ;
$kart_list  .= "<td align=left class=list_total>"."NT$".$price_sum."<td>" ;
$kart_list .= "</tr>";
$kart_list  .= "<tr>" ;
$kart_list  .= "<td align=right><input class=sumbit1 name=back type=button onClick=\"javascript:history.back(1)\" value=\"回上頁\"><td>" ;
$kart_list  .= "<td align=right colspan=2><input class=sumbit2  name=kart_buy type=button onclick=
\"
window.open('Mkart/kart_order.php', 'code', config='height=600, width=1000, location=no, toolbar=no, scrollbars=yes, resizable=yes, top=100, left=600');
window.location.href = '" . $str_tortoisePrjDistance . "index.php?option=com_content&view=frontpage&Itemid=60';
\"
value='前往結帳'><td>";
$kart_list .= "</tr>";
$kart_list  .= "</table>" ;
$kart_list  .= "</form>" ;

}else{
$kart_list  .= "<table width=100% border=0  id=frm_list>" ;
$kart_list  .= "<tr>" ;
$kart_list  .= "<td align=center class=list_name>"."商品名稱"."</td>" ;
$kart_list  .= "<td align=center class=list_amount>"."數量"."</td>" ;
$kart_list  .= "<td align=center class=list_sum>"."小計"."</td>" ;
$kart_list  .= "<td align=center>".""."</td>" ;
$kart_list  .= "</tr>" ;
$kart_list  .= "<tr><td colspan=4><hr><td></tr>" ;
$kart_list  .= "<tr><td colspan=4><input class=sumbit name=back type=button onClick=\"javascript:history.back(1)\" value=\"回上頁\"><td></tr>" ;
$kart_list  .= "</table>" ;
}

//$kart_list  .= print_r($_SESSION) ;
echo $kart_list ;
?>
